package edu.gatech.coc.sirana.detection;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import net.util.Debug;
import net.util.Out;

import edu.gatech.coc.sirana.graph.CycleDetector;
import edu.gatech.coc.sirana.graph.InteractionCycle;
import edu.gatech.coc.sirana.graph.OnlineGraph;
import edu.gatech.coc.sirana.graph.SRCycles;
import edu.gatech.coc.sirana.parser.ClientLogItem;
import edu.gatech.coc.sirana.parser.ClientLogParser;
import edu.gatech.coc.sirana.util.CONSTANT;

public class AttributeCreator {

	/**
	 * Find all cycles in healthy data.
	 * @throws IOException 
	 */
	public ArrayList<SRCycles> createSRCAttributeData( String dir, String postFix) throws IOException{	
		ArrayList<SRCycles> allSRCyles = new ArrayList<SRCycles>();
		String[] srcFiles = new File(dir).list();
		int numFile = 0;

		int id = 1;
		for (int i=0; i<srcFiles.length; i++){
//			Debug.println("\n===createSRCAttributeData from: " + srcFiles[i]);
			String f	= srcFiles[i];
			if (!f.endsWith(postFix)) continue;
			
//			HashMap<String, Integer> srCycleMap = new HashMap<String, Integer> ();
			ArrayList<SRCycles> srcs = findSRCyclesFromFile(dir+srcFiles[i]);
			allSRCyles.addAll(srcs);
			for (SRCycles s : srcs){
//				Debug.println("-------ATTR------");
				Debug.print(id++ + " ");
				Debug.print(s.getCycles().size()+ " ");
				Debug.println( 
//							"[" + 
							s.findIDSequence() 
//							+ "]"
							);
//				Debug.println( s.toString() );
				
//				Debug.exit();
			}
		
			numFile++;
		}
		Debug.println(numFile + " files checked in: \t " + dir );
		
		return allSRCyles;
				
	}
	
//	private SRCAttribute createSRCAttribute(SRCycles src, )
	
	private ArrayList<SRCycles> findSRCyclesFromFile(String file) throws IOException{	
		ClientLogParser clp	= new ClientLogParser(file);			
		ArrayList<ClientLogItem> interactionList = clp.parseLogItems();		
		
		CycleDetector cd = new CycleDetector();
		OnlineGraph og = new OnlineGraph();
		og.detectRecursiveInteractionCircles(interactionList);
		
		ArrayList<InteractionCycle> cycles = cd.findCyclesAndRepeatedSequence(interactionList);
		
		//////////////////////////////////////////////////////////////////		
		ArrayList<SRCycles> srcs	= cd.findSRCycles(cycles, og);
		
		for (SRCycles src : srcs){
			if (src.getCycles().size() < 2 ) continue;		
//			
//			String srcAction = src.findCharactorActionString();
////			Debug.println("___SRC: " + src );
////			Debug.println("\tfindCharactorActionString" + srcAction);
//			if (!srcMap.containsKey(srcAction)){
//				srcMap.put(srcAction, 1);
//			}else{
//				int occ = srcMap.get(srcAction);
//				srcMap.put(srcAction, occ+1);
//			}		
		}
		
		return srcs;
	}
	
	
	private int findCyclesOnlineFromFileList(String dir, String postFix, HashMap<String, Integer> cyclesInAllLogs) throws IOException{
		String[] files = new File(dir).list();
		int numAllCycles = 0;
		for (int i=0; i<files.length; i++){
			if (!files[i].endsWith(postFix)) continue;
//			Debug.println("Find all cycles from: " + files[i]);
			ClientLogParser clp	= new ClientLogParser(dir+files[i]);
			ArrayList<ClientLogItem> interactionList	= clp.parseLogItems();	
			
			ArrayList<InteractionCycle> cyclesInOneLog = new CycleDetector().findCyclesAndRepeatedSequence(interactionList);
			numAllCycles += cyclesInOneLog.size();
			
			for (InteractionCycle ic: cyclesInOneLog){
				String actions = ic.findAllAction().toString();
				
				if (cyclesInAllLogs.containsKey(actions)){
					int occurrence = cyclesInAllLogs.get(actions);
					occurrence++;
					cyclesInAllLogs.put(actions, occurrence);
				}else{
					cyclesInAllLogs.put(actions, 1);					
				}
			}
		}
		
//		Debug.println(cyclesInAllLogs.size() + " in created method of cycles map.");
		return numAllCycles;
	}
}
